<template>

	<a-row :gutter="10">

		<a-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
			<a-card :bordered="false" >
				<a-tree
					v-if="treeData?.length > 0"
					:defaultExpandAll="true"
					:tree-data="treeData"
					:field-names="{ children: 'children', title: 'name', key: 'id' }"
					@select="treeSelect">
				</a-tree>
				<div v-else>暂无分类</div>
			</a-card>
		</a-col>

		<a-col :xs="24" :sm="24" :md="24" :lg="19" :xl="19">
			<a-card :bordered="false">
				<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
					<a-row :gutter="24">
						<a-col :span="6">
							<a-form-item label="供应商" name="supplierId">
								<a-select v-model:value="searchFormState.supplierId"
										  :field-names="{label: 'supplierName', value: 'supplierId'}"
										  placeholder="请选择操作类型" allow-clear :options="supplierData" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
							<a-button style="margin: 0 8px" @click="reset">重置</a-button>
						</a-col>
					</a-row>
				</a-form>

				<s-table
					ref="tableRef"
					:columns="columns"
					:data="loadData"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:tool-config="toolConfig"
					:row-selection="options.rowSelection">\

					<template #operator class="table-operator">
						<a-space>
							<a-button @click="exportBatchUser">
								<template #icon><export-outlined /></template>
								{{ $t('user.batchExportButton') }}
							</a-button>

						</a-space>
					</template>

				</s-table>
			</a-card>
		</a-col>
	</a-row>
</template>

<script setup name="realtimeinventory">
	import imsRealTimeInventoryApi from '@/api/ims/imsInventoryApi'
	import imsWarehouseApi from "@/api/ims/imsWarehouseApi";
	import {cloneDeep} from "lodash-es";
	import downloadUtil from '@/utils/downloadUtil'
	import imsSupplierApi from "@/api/ims/imsSupplierApi";

	const treeData = ref([])
	const searchFormState = ref({})
	const searchFormRef = ref({})
	const supplierData = ref([])

	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '仓库',
			dataIndex: 'warehouse.wareName',
			customRender: ({ record }) => record.warehouse?.wareName

		},
		{
			title: '商品',
			dataIndex: 'product.productname',
			customRender: ({ record }) => record.product?.productName
		},
		{
			title: '计量单位',
			dataIndex: 'unitId'
		},
		{
			title: '库存数量',
			dataIndex: 'count'
		},
		{
			title: '成本',
			dataIndex: 'cost'
		},
		{
			title: '供应商',
			dataIndex: 'supplier.suppliername',
			customRender: ({ record }) => record.supplier?.supplierName
		},
		// {
		// 	title: '状态',
		// 	dataIndex: 'status'
		// }
	];
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys.length > 0) {
			searchFormState.value.wareId = selectedKeys.toString()
		} else {
			delete searchFormState.value.wareId
		}
		tableRef.value.refresh(true)
	}

	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		imsWarehouseApi.wareTree(searchFormParam).then(res => {
			treeData.value = res
		})


		return imsRealTimeInventoryApi.imsRealTimeInventoryPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 批量导出
	const exportBatchUser = () => {
		imsRealTimeInventoryApi.export().then((res) => {
			downloadUtil.resultDownload(res)
			tableRef.value.clearSelected()
		})
	}


	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteImsRealTimeInventory = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		imsRealTimeInventoryApi.imsRealTimeInventoryDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchImsRealTimeInventory = (params) => {
		imsRealTimeInventoryApi.imsRealTimeInventoryDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}

	onMounted(() => {
		// 获取供应商/客户
		imsSupplierApi.imsSupplierPage({
			current: 1,
			size: 9999
		}).then(res => {
			// 供应商名称 = 供应商编号+供应商名称
			supplierData.value = res.records
			console.log('获取的供应商：', res.records )
		})
	})

</script>
